/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.vranckaert.calendar.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
import eu.vranckaert.calendar.R;

/**
 * Custom view that draws a vertical time "ruler" representing the chronological
 * progression of a single day. Usually shown along with {@link com.realdolmen.trm.util.ui.cal.widget.BlockView}
 * instances to give a spatial sense of time.
 */
public class MonthGrid extends View {
    private int mDividerColor = Color.LTGRAY;

    public MonthGrid(Context context) {
        this(context, null);
    }

    public MonthGrid(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MonthGrid(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimeRulerView,
                defStyle, 0);

        mDividerColor = a.getColor(R.styleable.TimeRulerView_dividerColor, mDividerColor);

        a.recycle();
    }

    private Paint mDividerPaint = new Paint();

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final Paint dividerPaint = mDividerPaint;
        dividerPaint.setColor(mDividerColor);
        dividerPaint.setStyle(Style.FILL);

        final int right = getRight();

        // Walk left side of canvas drawing timestamps
        final int rows = 6;
        final int columns = 7;
        final int rowHeight = getHeight() / rows;
        final int columnWidth = getWidth() / columns;

        // Draw horizontal lines between each week
        for (int i = 0; i < rows; i++) {
            final int dividerY = rowHeight * i;
            canvas.drawLine(0, dividerY, right, dividerY, dividerPaint);
        }

        // Draw vertical lines between each day
        for (int c = 0; c < columns; c++) {
            int leftSpacing = columnWidth * c;
            canvas.drawLine(leftSpacing, 0, leftSpacing, getHeight(), dividerPaint);
        }
    }
}
